
export const arrayFindIndex = function(arr, pred) {
    for (let i = 0; i !== arr.length; ++i) {
        if (pred(arr[i])) {
        return i;
        }
    }
    return -1;
}

export const arrayFind = function(arr, pred) {
    const idx = arrayFindIndex(arr, pred);
    return idx !== -1 ? arr[idx] : undefined;
}

/**
 * 输入字数限制
 * @param {String} text 进行处理的文本
 * @param {Number} omitMode 处理方式 1:超出截掉 2:超出截掉+'...'
 * @param {Number} limit
 */
export const limitText = function({text, omitMode, limit}) {
    if(omitMode === undefined) omitMode = 2;
    if(limit === undefined) limit = 20;
    let cLen = 0;
    let resultText = '';
    for(let i=0; i<text.length; i++) {
        cLen++;
        let str = text[i];
        let matcher = str.match(/[^\x00-\xff]/g);
        let wlen = (matcher && matcher.length) || 0;
        cLen += wlen;
        if(cLen > limit * 2) {
            if(omitMode == 2) {
                resultText += '...';
            }
            return resultText;
        }
        resultText += str;
    }
    return resultText;
}

/**
 * 输入字数限制
 * @param {String} text
 * @param {Number} limit
 */
export const wordRestriction = function(text, limit) {
    if (limit === undefined) limit = 20;
    let matcher = text.match(/[^\x00-\xff]/g);
    let wlen = (matcher && matcher.length) || 0;
    return Math.floor((limit * 2 - text.length - wlen) / 2);
}
